package com.jspgou.cms.manager.impl;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jspgou.cms.dao.ReportDao;
import com.jspgou.cms.entity.Order;
import com.jspgou.cms.entity.Report;
import com.jspgou.cms.entity.ReportType;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.manager.OrderMng;
import com.jspgou.cms.manager.ReportMng;
import com.jspgou.cms.manager.ReportTypeMng;
import com.jspgou.cms.manager.ShopMemberMng;
import com.jspgou.cms.manager.StoreMng;
import com.jspgou.common.hibernate3.Updater;
import com.jspgou.common.page.Pagination;
import com.jspgou.core.entity.Website;

@Service
@Transactional
public class ReportMngImpl implements ReportMng {
	@Transactional(readOnly = true)
	public Pagination getPage(int pageNo, int pageSize) {
		Pagination page = dao.getPage(pageNo, pageSize);
		return page;
	}
	
	@Transactional(readOnly = true)
	public Pagination getPage(Long memberId,Long storeId,int pageNo, int pageSize) {
		Pagination page = dao.getPage(memberId,storeId,pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public Report findById(Long id) {
		Report entity = dao.findById(id);
		return entity;
	}

	public Report save(Report bean) {
		dao.save(bean);
		return bean;
	}
	
	public Report save(Report bean,Website web,Long orderId,Long memberId,Long typeId){
		Order order=orderMng.findById(orderId);
		bean.setOrder(order);
		if(order.getStore()!=null){		
			bean.setStore(order.getStore());
		}else{
			bean.setStore(null);
		}
		bean.setWebsite(web);
		ShopMember member=shopMemberMng.findById(memberId);
		bean.setMember(member);
		ReportType reportType=reportTypeMng.findById(typeId);
		bean.setReportType(reportType);
		bean.setStartDate(new Date());
		Long date=new Date().getTime()+member.getId();
		bean.setCode(date.toString());
		dao.save(bean);
		return bean;
	}

	public Report update(Report bean) {
		Updater<Report> updater = new Updater<Report>(bean);
		Report entity = dao.updateByUpdater(updater);
		return entity;
	}

	public Report deleteById(Long id) {
		Report bean = dao.deleteById(id);
		return bean;
	}
	
	public Report[] deleteByIds(Long[] ids) {
		Report[] beans = new Report[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private ReportDao dao;
	@Autowired
	private OrderMng orderMng;
	@Autowired
	private ReportTypeMng reportTypeMng;
	@Autowired
	private ShopMemberMng shopMemberMng;
	@Autowired
	private StoreMng storeMng;

	@Autowired
	public void setDao(ReportDao dao) {
		this.dao = dao;
	}
}